// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./clientManage_belongs.css");
	var tools = require("tools");
	var isCard;
	var constant = require("constant");
	var tools = require("tools");
	var interestRate;
	var penalInterest;
	
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var enterpriseId = "";
	var businessLicenceImage = "";
	var certificateImage = "";
	var legalPersonFrontImage = "";
	var legalPersonBackImage = "";
	var qualificationsImage = "";
	var creditReportImage = "";
	var init = function(page) {
		enterpriseId = page.params.enterpriseId;
		$(".nav_active").removeClass("nav_active1");
		$(".financeBelongs").addClass("nav_active1");
		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}
		
		inquireOwnEnterprisePageList();

		//点击企业基本信息
		$(".clientManage_details1 .enterpriseInfo").on("click", function() {
			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
		});

		$(".financeSituation").on("click", function() {
			router.route("client/clientManage_financeSituation.html?enterpriseId=" + enterpriseId);
		});

		//证明材料
		$(".financeEvidentiary").on("click", function() {
			router.route("client/clientManage_financeEvidentiary.html?enterpriseId=" + enterpriseId);
		});
		
		//点击企业基本信息
		$(".enterpriseInfo").on("click", function() {
			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
		});
		
		//点击实名认证
		$(".authenticationState").on("click", function() {
			router.route("client/clientManage_authentication.html?enterpriseId=" + enterpriseId, true);
		});
		
		$("#clientManage_belongs .table-search-button").unbind("click").click(function(){
			inquireOwnEnterprisePageList();
		});
		
		$("#clientManage_belongs .tradeType a").unbind("click").click(function(){
			$("#clientManage_belongs input[name=tradeType]").val($(this).attr("tradeType"));
			pageIndex = 1;
			$("#clientManage_belongs .tradeType a").removeClass("tradeTypeActive");
			$(this).addClass("tradeTypeActive");
			inquireOwnEnterprisePageList();
		});
	};
	
	//获取企业用户列表
	var inquireOwnEnterprisePageList = function(){
		var body = base.parseForm($("#clientManage_belongs .table-search"));
		body.createDateTimeBegin = body.createDateTimeBegin.split("-").join("");
		body.createDateTimeEnd = body.createDateTimeEnd.split("-").join("");
        base.fnAjax({
            action: "inquireOwnEnterprisePageList.json",
            oData: {
            	enterpriseType: 0,
            	pageIndex: pageIndex,
                pageSize: pageSize,
                mobile:body.mobile,
                enterpriseName:body.enterpriseName,
                legalPerson:body.legalPerson,
                state:body.state,
                auditState:body.auditState,
                createDateTimeBegin:body.createDateTimeBegin,
                createDateTimeEnd:body.createDateTimeEnd,
                enterpriseId: enterpriseId,
                tradeType: body.tradeType
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#clientManage_belongs script").html();
                    var html = juicer(tpl, data);
                    $("#clientManage_belongs tbody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#clientManage_belongs .pagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireOwnEnterprisePageList();
                    });
                    
                     //点击详情
			        $("#clientManage_belongs").on("click",".table_details_link",function(){
			           var enterpriseIds = [];
			           var $this = $(this).parents("tr.tr_style");
					   enterpriseIds.push($this.attr("enterpriseId"));
						router.route("client/clientManage_details.html?enterpriseId="+enterpriseIds,true);
			        });
			            //点击删除
			        $("#clientManage_belongs").on("click",".table_delete_btn",function(){
			        	$("#popdialog").html(html_delete);
			           var enterpriseIds = [];
			           var $this = $(this).parents("tr.tr_style");
					   enterpriseIds.push($this.attr("enterpriseId"));
			           disableEnterprise(enterpriseIds);
			        });
			        
			        //禁用企业
					$("#clientManage_belongs .table_forbidden_btn").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("禁用企业", "是否确定禁用该企业？", function(){
							forbidEnterprise($this.parents("tr").attr("enterpriseId"));
						});
					});
					
					//冻结企业
					$("#clientManage_belongs .table_freeze_btn").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("冻结企业", "是否确定冻结该企业？", function(){
							freezeEnterprise1($this.parents("tr").attr("enterpriseId"));
						});
					});
					
					//启用企业
					$("#clientManage_belongs .table_enable_btn").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("启用企业", "是否确定启用该企业？", function(){
							enableEnterprise($this.parents("tr").attr("enterpriseId"));
						});
					});
					
					tools.inquireCurrentOperationList($("#main"));
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
	return {
		init: init
	};
});